package com.qf.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.core.dto.PageDto;
import com.qf.core.vo.PageResult;
import com.qf.core.vo.R;
import com.qf.industry.entity.Industry;
import com.qf.product.entity.Tenant;
import com.qf.product.mapper.TenantMapper;
import com.qf.product.service.TenantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 租户表 服务实现类
 * </p>
 *
 * @author yxh
 * @since 2023-10-26
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements TenantService {

    @Override
    public PageResult<Tenant> page(PageDto<Tenant> pageDto) {

        //分页信息
        IPage<Tenant> page = new Page<>(pageDto.getNow(),pageDto.getSize());

        //条件
        QueryWrapper<Tenant> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(pageDto.getWhere().getName())){
            queryWrapper.lambda().like(Tenant::getName,pageDto.getWhere().getName());
        }

        if (!StringUtils.isEmpty(pageDto.getWhere().getAccessKey())){
            queryWrapper.lambda().like(Tenant::getAccessKey,pageDto.getWhere().getAccessKey());
        }

        IPage<Tenant> pageInfo = this.baseMapper.selectPage(page, queryWrapper);

        //封装分页结果
        PageResult<Tenant> pageResult = new PageResult<>();
        pageResult.setPages(pageInfo.getPages());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setList(pageInfo.getRecords());

        return pageResult;
    }

    @Override
    public R updateTen(Tenant tenant) {
        if (tenant.getEnable()==1){
            tenant.setEnable(2);
        }else {
            tenant.setEnable(1);
        }
        int i = this.baseMapper.updateById(tenant);
        if (i>0){
            return R.ok("修改成功");
        }
        return R.fail("禁用成功");
    }
}
